package com.bxd.baodan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.bxd.baodan.R;
import com.bxd.baodan.adapter.CommonAdapter;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.item.QesTagBean;
import com.bxd.baodan.item.SortItem;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.widget.MyListView;

import java.util.ArrayList;
import java.util.List;

public class QuestionTagActivity extends BaseActivity {

    private MyListView myListView;
    private CommonAdapter<SortItem> adapter;
    private List<SortItem> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_tag);
        setActionBarTitle("问题分类");

        initView();
        setAdapter();
        initData();

    }

    private void initData() {
        NetUtil netUtil = new NetUtil(this, JsonApi.QUESTION_OPTIONS);
        netUtil.postRequest("", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {
                QesTagBean qesTagBean = new Gson().fromJson(result, QesTagBean.class);
                List<SortItem> tag = qesTagBean.data.list.tag;
                List<SortItem> tag27 = qesTagBean.data.list.tag27;
                mList.clear();
                if (tag != null) {
                    mList.addAll(tag);
                }
                if (tag27 != null) {
                    mList.addAll(tag27);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void setAdapter() {
        adapter = new CommonAdapter<SortItem>(this, mList, R.layout.questin_tag_item) {
            @Override
            public void convert(ViewHolder holder, final SortItem item) {
                holder.setText(R.id.tv_tag, item.name);
                holder.getView(R.id.relative_bottom).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("tagId", item.tid);
                        intent.putExtra("tagName", item.name);
                        setResult(0, intent);
                        finish();
                    }
                });

            }
        };
        myListView.setAdapter(adapter);
    }

    private void initView() {
        myListView = (MyListView) findViewById(R.id.listView1);
    }

}
